package xjy.smartcommunity.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

import xjy.smartcommunity.R;
import xjy.smartcommunity.util.HttpUtils;
import xjy.smartcommunity.util.SharedPrefUtils;

/**
 * Created by victorzhang on 2017/8/4.
 * 用于只读的形式显示个人信息
 */
public class PinfoShowActivity extends AppCompatActivity {

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personinfo_show);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//启用返回按钮

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                PersonInfoshow();
            }
        });
        thread.start();
    }

    private void PersonInfoshow() {
        String urlStr = getString(R.string.server_host) + "/getUserByUsername.do";
        String errMsg = "";
        Bundle bundle = new Bundle();
        try {
            HashMap<String, String> paras = new HashMap<>();
            String username = SharedPrefUtils.getValFromSP(this, SharedPrefUtils.username);
            paras.put("username", username);
            JSONObject json = HttpUtils.requestJson(this, urlStr, paras, false);
            if (json.getInt("status") != 0)
                throw new Exception(json.getString("message"));
            bundle.putString("username", username);
            JSONArray jsonArray = json.getJSONArray("data");
            if (jsonArray.length() == 0)
                throw new Exception("当前用户不存在");
            JSONObject user = jsonArray.getJSONObject(0);
            bundle.putString("name", user.getString("name"));
            int sex = user.getInt("sex");
            if (sex == 0)
                bundle.putString("sex", "男");
            else
                bundle.putString("sex", "女");
            bundle.putString("age", String.valueOf(user.getInt("age")));
            bundle.putString("tel", user.getString("tel"));
            bundle.putString("address",user.getString("address"));
        } catch (Exception ex) {
            errMsg = ex.getMessage();
            if (errMsg == null || errMsg.length() == 0)
                errMsg = "请求失败";
        }
        bundle.putString("errMsg", errMsg);
        Message msg = new Message();
        msg.setData(bundle);
        showPersonalInfoHandler.sendMessage(msg);
    }

    private Handler showPersonalInfoHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String errMsg = msg.getData().getString("errMsg");
            if (errMsg.length() > 0) {
                Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_SHORT).show();
                return;
            }
            TextView username = (TextView) findViewById(R.id.personuser);
            TextView name = (TextView) findViewById(R.id.personname);
            TextView sex = (TextView) findViewById(R.id.personsex);
            TextView age = (TextView) findViewById(R.id.personage);
            TextView tel = (TextView) findViewById(R.id.persontel);
            TextView address =(TextView) findViewById(R.id.persomadr);


            Bundle bundle = msg.getData();
            username.setText(bundle.getString("username"));
            name.setText(bundle.getString("name"));
            sex.setText(bundle.getString("sex"));
            age.setText(bundle.getString("age"));
            tel.setText(bundle.getString("tel"));
            address.setText(bundle.getString("address"));
        }
    };
}
